﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.DomainLayer;

using System.Collections.ObjectModel;
namespace HA.ModFlow.Controls
{
   public class PCDrainCollecion:PCBase 
    {
       public PCDrainCollecion(  DrainCollection Drain)
       {
           this._DrainCollection = Drain;
           this._periodStep = new List<int>();
           for (int i = 0; i < Drain.NPeriods; i++)
           {
               this._periodStep.Add(i + 1);
           }
           this._lstUsePrecious = new List<bool>();
           this._lstUsePrecious.AddRange(Drain.IsUsePrecious);
           this.Drains = new ObservableCollection<PCDrain>();
           foreach (Drain  item in Drain .InnerList)
           {
               PCDrain pc = new PCDrain((Drain)item.Clone());
               this._Drains.Add(pc);
           }

       }

       private DrainCollection _DrainCollection;

       bool _isShowId = true;

       public bool IsShowId
       {
           get { return _isShowId; }
           set { _isShowId = value; }
       }

       List<bool> _lstUsePrecious;
       public bool UsePreciousEnabled
       {
           get
           {
               if (this._currentPeriod == 0)
               {
                   return false;
               }
               else
               {
                   return true;
               }
           }
       }

       List<int> _periodStep;

       public List<int> PeriodStep
       {
           get { return _periodStep; }
       }

       //具体的index值

       int _currentPeriod = 0;

       public int CurrentPeriod
       {
           get { return _currentPeriod; }
           set
           {
               _currentPeriod = value;
               this.UsePrecious = this._DrainCollection.IsUsePrecious[_currentPeriod];
               foreach (PCDrain  item in this._Drains)
               {
                   item.CurrentIndex = this._currentPeriod;
               }
               this.OnPropertyChanged("UsePreciousEnabled");
               this.OnPropertyChanged("UsePrecious");
           }
       }

       public bool UsePrecious
       {
           get
           {
               if (_lstUsePrecious.Count == 0)
                   return false;
               return this._lstUsePrecious[this._currentPeriod];
           }
           set
           {
               this._lstUsePrecious[this._currentPeriod] = value;
               this._DrainCollection.IsUsePrecious[this._currentPeriod] = value;
               this.OnPropertyChanged("UsePrecious");
           }
       }
    

       ObservableCollection<PCDrain> _Drains;

       public ObservableCollection<PCDrain> Drains
       {
           get { return _Drains; }
           set { _Drains = value; }
       }

       public void ConfirmData()
       {
           List<Drain> drains = new List<Drain>();
           foreach (PCDrain item in this._Drains)
           {
               drains.Add(item.Drain );
           }
           this._DrainCollection.ResetDrains (drains);
           this._DrainCollection.ResetIsUsePrecious(this._lstUsePrecious);
       }

       /// <summary>
       /// 检查层行列的正确定性
       /// </summary>
       /// <param name="row"></param>
       /// <param name="col"></param>
       /// <param name="layer"></param>
       /// <param name="index"></param>
       /// <param name="isIndexId"></param>
       /// <returns></returns>
       public bool CheckInputIJKorIndex(int row, int col, int layer, int index, bool isIndexId)
       {
           foreach (PCDrain item in this._Drains)
           {
               if (!isIndexId)
               {
                   if (item.I <= 0 || item.I > row)//行
                   {
                       System.Windows.MessageBox.Show("不存在第 " + item.I + " 行，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                       return false;
                   }
                   if (item.J <= 0 || item.J > col)
                   {
                       System.Windows.MessageBox.Show("不存在第 " + item.J + " 列，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                       return false;
                   }
                   if (item.K <= 0 || item.K > layer)
                   {
                       System.Windows.MessageBox.Show("不存在第 " + item.K + " 层，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                       return false;
                   }
               }
               else
               {
                   if (item.Index <= 0 || item.Index > index)
                   {
                       System.Windows.MessageBox.Show("不存在 " + item.Index + " 单元格ID，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                       return false;
                   }

               }
           }

           return true;
       }

       public void CreateNew()
       {
           Drain drain = new Drain(1, 1, 1, this._DrainCollection.Nrows, this._DrainCollection.Ncols, this._DrainCollection.Nlayers, this._periodStep.Count);

           PCDrain pcNew = new PCDrain(drain);

           this.Drains.Add(pcNew);
       }


    }

   public class PCDrain : PCBase
   {


       public PCDrain(Drain drain)
       {
           this._Drain = drain;
       }

       Drain _Drain;
       public Drain Drain
       {
           get { return _Drain; }          
       }
       
       int _currentIndex = 0;

       public int CurrentIndex
       {
           get { return _currentIndex; }
           set
           {
               _currentIndex = value;
               //this.OnPropertyChanged("Stage");
               this.OnPropertyChanged("Conductance");
               this.OnPropertyChanged("Elevation");
           }
       }
       public int Index
       {
           get
           {
               return this._Drain.Index;
           }
           set
           {
               this._Drain.Index = value;
               this.OnPropertyChanged("I");
               this.OnPropertyChanged("J");
               this.OnPropertyChanged("K");
           }
       }



       public int I
       {
           get { return this._Drain.I; }
           set
           {
               this._Drain.I = value;
               this.OnPropertyChanged("Index");
           }
       }


       public int J
       {
           get { return this._Drain.J; }
           set
           {
               this._Drain.J = value;
               this.OnPropertyChanged("Index");
           }
       }


       public int K
       {
           get { return this._Drain.K; }
           set
           {
               this._Drain.K = value;
               this.OnPropertyChanged("Index");
           }
       }


       //public double Stage
       //{
       //    get { return this._Drain.[this._currentIndex]; }
       //    set { this._Drain.Stage[this._currentIndex] = value; }
       //}


       public double Conductance
       {
           get { return this._Drain.Conductance[this._currentIndex]; }
           set { this._Drain.Conductance[this._currentIndex] = value; }
       }

       public double Elevation
       {
           get { return this._Drain.Elevation[this._currentIndex]; }
           set { this._Drain.Elevation[this._currentIndex] = value; }
       }

   }
}
